from rest_framework import serializers

from .models import Essay, Picture, Comment, Label


class CommentSerializer(serializers.ModelSerializer):
    nick_name = serializers.CharField
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    rich_text = serializers.CharField
    parent_id = serializers.CharField

    class Meta:
        model = Comment
        fields = ('id', 'nick_name', 'create_time', 'rich_text', 'parent_id')


class PictureSerializer(serializers.ModelSerializer):
    picture_name = serializers.CharField
    order = serializers.IntegerField
    picture_url = serializers.CharField

    class Meta:
        model = Picture
        fields = ('order', 'picture_name', 'picture_url')


class EssaySerializers(serializers.ModelSerializer):
    labels = serializers.StringRelatedField(many=True)
    creator = serializers.StringRelatedField(many=False)
    series = serializers.StringRelatedField(many=False)
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    essay_picture = PictureSerializer(many=True, read_only=True)
    essay_comment = CommentSerializer(many=True, read_only=True)

    class Meta:
        model = Essay
        fields = ('id', 'title', 'summary', 'essay_picture', 'essay_comment', 'labels', 'content', 'html_content', 'rich_text',
                  'create_time', 'update_time', 'creator', 'series')


class EssayDescSerializers(serializers.ModelSerializer):
    labels = serializers.StringRelatedField(many=True)
    creator = serializers.StringRelatedField(many=False)
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    essay_picture = PictureSerializer(many=True, read_only=True)

    class Meta:
        model = Essay
        fields = ('id', 'title', 'summary', 'essay_picture', 'labels', 'create_time', 'update_time', 'creator')


class EssayListSerializers(serializers.ModelSerializer):
    labels = serializers.StringRelatedField(many=True)
    creator = serializers.StringRelatedField(many=False)
    essay_picture = PictureSerializer(many=True, read_only=True)
    # essay_comment = CommentSerializer(many=True, read_only=True)
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')

    class Meta:
        model = Essay
        fields = ('id', 'title', 'summary', 'labels', 'essay_picture', 'creator', 'create_time', 'update_time')


class SeriesSerializers(serializers.ModelSerializer):
    series_name = serializers.StringRelatedField(many=False)
    series_description = serializers.StringRelatedField(many=False)
    essay_series = EssayDescSerializers(many=True, read_only=True)
    # essay_comment = CommentSerializer(many=True, read_only=True)
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')

    class Meta:
        model = Essay
        fields = ('essay_series', 'series_name', 'series_description', 'create_time', 'update_time')


class SeriesListSerializers(serializers.ModelSerializer):
    series_name = serializers.StringRelatedField(many=False)
    series_description = serializers.StringRelatedField(many=False)
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')

    class Meta:
        model = Essay
        fields = ('id', 'series_name', 'series_description', 'create_time', 'update_time')


class LabelListSerializers(serializers.ModelSerializer):
    title = serializers.StringRelatedField(many=False)

    class Meta:
        model = Label
        fields = ('id', 'title')
